<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item v-if="dataForm.id" label="工号" prop="code">
      <el-input v-model="dataForm.code" placeholder="工号"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="教师名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="教师名称"></el-input>
    </el-form-item>
    <el-form-item label="主讲课程" prop="main">
      <el-input v-model="dataForm.main" placeholder="主讲课程"></el-input>
    </el-form-item>
    <el-form-item label="辅讲课程" prop="minor">
      <el-input v-model="dataForm.minor" placeholder="辅讲课程"></el-input>
    </el-form-item>
    <el-form-item label="联系方式" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="联系方式"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          code: '',
          password: '',
          name: '',
          main: '',
          minor: '',
          phone: '',
          createTime: '',
          updateTime: '',
          isDelete: ''
        },
        dataRule: {
          code: [
            { required: true, message: '工号不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '教师名称不能为空', trigger: 'blur' }
          ],
          main: [
            { required: true, message: '主讲课程不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '联系方式不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/edm/teacher/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.code = data.teacher.code
                this.dataForm.password = data.teacher.password
                this.dataForm.name = data.teacher.name
                this.dataForm.main = data.teacher.main
                this.dataForm.minor = data.teacher.minor
                this.dataForm.phone = data.teacher.phone
                this.dataForm.createTime = data.teacher.createTime
                this.dataForm.updateTime = data.teacher.updateTime
                this.dataForm.isDelete = data.teacher.isDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/edm/teacher/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'code': this.dataForm.code,
                'password': this.dataForm.password,
                'name': this.dataForm.name,
                'main': this.dataForm.main,
                'minor': this.dataForm.minor,
                'phone': this.dataForm.phone,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'isDelete': this.dataForm.isDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
